#ifndef SROS_DEVICE_DETAIL_RFID_IMPL_HPP
#define SROS_DEVICE_DETAIL_RFID_IMPL_HPP

#include "devices/detail/proto.hpp"
#include "devices/detail/define.hpp"

#include <commons/type/ret.hpp>

#include <stdint.h>
#include <memory>
#include <mutex>

namespace sros::device::detail
{
    class rfid_impl
    {
    public:
        struct DataUp
        {
            int32_t     id = -1;            // 最近一次触发的RFID ID
            bool        detecting = false;  // 是否处于一直触发状态
            uint32_t    error = 0;          // 错误码
            uint8_t     state = DEV_UNINIT; // 状态: 0 未初始化 1 初始化中 2 运行中 3 断联 4 错误
        };

        using EventCb = std::function<void(const uint8_t &ev_id, const uint32_t &data, const std::string &msg)>;

        rfid_impl() = default;
        virtual ~rfid_impl() = default;

        // 初始化
        virtual void init() = 0;

        // 运行
        virtual void run() = 0;

        // 关闭
        virtual void close() = 0;

        // 解析反馈报文
        virtual void parse(const device::proto::RspUartMsg &uart_msg) {}

        // 上报的数据
        DataUp data_up() { std::lock_guard<std::mutex> lck(mtx_); return data_up_; }

        // 更新上报的数据
        void set_data_up(const DataUp &data) { std::lock_guard<std::mutex> lck(mtx_); data_up_ = data; }

        // 绑定的串口索引
        inline uint16_t uart_index() { return uart_index_; }

        // 设备信息
        inline Info info() { return info_; }

        // 事件触发
        inline void install_event(EventCb cb) { event_cb_ = cb; }

    protected:
        std::mutex     mtx_;
        EventCb        event_cb_;
        DataUp         data_up_;
        Info           info_;
        uint16_t       uart_index_;
    };
}

#endif